﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using web.Models;
using web.Common;
using web.BLL;

namespace web.Controllers
{
    public class NewsController : BaseController
    {
        //
        // GET: /News/

        public ActionResult about()
        {
            ViewBag.title = "关于我们";
            ViewBag.id = 1;
            return View();
        }

        public ActionResult info()
        {
            ViewBag.id = RequestQ("id") != "" ? Convert.ToInt32(RequestQ("id")) : 0;
            ViewBag.typeid = RequestQ("typeid") != "" ? Convert.ToInt32(RequestQ("typeid")) : 0;
            return View();
        }

        public ActionResult contact()
        {
            ViewBag.title = "联系我们";
            ViewBag.id = 2;
            return View();
        }

        public ActionResult newslist(int id)
        {
            ViewBag.typeid = id;
            return View();
        }

        public ActionResult product()
        {
            return View();
        }
        public ActionResult typeinfo()
        {
            return View();
        }

        [ValidateInput(false)]
        public JsonResult saveNews()
        {

            int id = RequestQ("id") != "" ? Convert.ToInt32(RequestQ("id")) : 0;
            //编辑
            return Json(sitebll.addorupdatenews(id));
        }

        public JsonResult getinfo()
        {
            int id = RequestQ("id") != "" ? Convert.ToInt32(RequestQ("id")) : 0;
            //编辑

            return Json(BLL.sitebll.getnews(id));

        }

        public ActionResult getinfolist()
        {
            int typeid = RequestQ("typeid") != "" ? Convert.ToInt32(RequestQ("typeid")) : 0;
            string keyword = RequestQ("keyword");
            int start = RequestQ("start") != "" ? Convert.ToInt32(RequestQ("start")) : 0;
            int limit = RequestQ("limit") != "" ? Convert.ToInt32(RequestQ("limit")) : 20;
            return Content(sitebll.getnewslist(typeid,keyword,start,limit));
        }

        public JsonResult delnews()
        {
            int id = RequestQ("id") != "" ? Convert.ToInt32(RequestQ("id")) : 0;

            try
            {
                sitebll.delnews(id);
                return Json("{\"success\":true,\"msg\":\"删除成功\"}");
            }
            catch {

                return Json("{\"success\":false,\"msg\":\"删除失败\"}");
            }
        
        }


        public JsonResult batchdelnews()
        {
            string ids = RequestQ("ids");

            try
            {
                sitebll.delnews(ids);
                return Json("{\"success\":true,\"msg\":\"删除成功\"}");
            }
            catch
            {

                return Json("{\"success\":false,\"msg\":\"删除失败\"}");
            }

        }

        #region type

        public ActionResult addtype()
        {
            ViewBag.id = RequestQ("id") != "" ? Convert.ToInt32(RequestQ("id")) : 0;
            return View();
        }

        public ActionResult gettypelist()
        {
            int start = RequestQ("start") != "" ? Convert.ToInt32(RequestQ("start")) : 0;
            int limit = RequestQ("limit") != "" ? Convert.ToInt32(RequestQ("limit")) : 20;
            return Content(sitebll.gettypelist(start, limit));
        }
        public JsonResult batchdeltype()
        {
            string ids = RequestQ("ids");

            try
            {
                sitebll.deltypes(ids);
                return Json("{\"success\":true,\"msg\":\"删除成功\"}");
            }
            catch
            {

                return Json("{\"success\":false,\"msg\":\"删除失败\"}");
            }
        }

        public JsonResult savetype()
        {

            int id = RequestQ("id") != "" ? Convert.ToInt32(RequestQ("id")) : 0;
            
            //编辑
            return Json(sitebll.savetype(id));
        }
        public JsonResult gettypeinfo()
        {
            int id = RequestQ("id") != "" ? Convert.ToInt32(RequestQ("id")) : 0;
            //编辑
            return Json(sitebll.gettype(id));

        }
        #endregion


    }
}
